package edu.pku.ss.taas.cloud.monitor.lightweight;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class Monitor extends Thread {
	private MonitorClientManager cache = MonitorClientManager.getInstance();
	private static Boolean isShuttingDown = false;
	private static CpuGather cg = new CpuGather();
	private static MemGather mg =  new MemGather();
	private static DiskGather dg =  new DiskGather();
	private static NetworkGather ng =  new NetworkGather();
	
//	private static boolean isFirstTime = true;
	
	public Monitor(){}
	
	@Override
	public void run() {
		System.out.println("begin to monitor system:"+new Date());
		String hostIp = getHostIp();
		System.out.println("begin to monitor system: hostIp = "+hostIp);
		while (!isShuttingDown) {
			// report monitor data to memcache server
			Map<String, Object> result = collectMonitorDatas();
			
			if(!cache.keyExists(hostIp))
				cache.add(hostIp, result,new Date(System.currentTimeMillis()+1000*60*3));
			else
				cache.replace(hostIp, result,new Date(System.currentTimeMillis()+1000*60*3));
			isShuttingDown = Boolean.TRUE.equals(cache
					.get(Constants.SHUTTING_DOWN_COMMAND));
			System.out.println("*******************************8"+new Date()+":"+result);
			try {
				Thread.currentThread().sleep(1000*5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("is the monitor to quit:" + new Date());
	}

	public Map<String, Object> collectMonitorDatas() {
		Map<String, Object> result = new HashMap<String, Object>();

		result.putAll(cg.getCpuInfo());
		result.putAll(mg.getMemInfo());
		result.putAll(dg.getDiskInfo());
		result.putAll(ng.getNetworkInfo());

		System.out.println("collectMonitorDatas:" + result);
		return result;
	}
	
	public String getHostIp(){
		return MonitorUtil.getLocalIP();
	}




	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("going to monitor system:"+new Date());
		Monitor monitor = new Monitor();
		System.out.println("ip:" + monitor.getHostIp());
		monitor.start();
		
//		System.out.println(new Monitor().collectMonitorDatas());
		
//		while(!isShuttingDown){
//			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//			try {
//				System.out.println("please input q to exit:");
//				String input = br.readLine();
//				
//				System.out.println("read input:"+input);
//				if("q".equals(input)){
//					System.out.println("bye!");
//					isShuttingDown = true;
//				}
//				
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
		
	}
	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		System.out.println("going to monitor system:"+new Date());
//		
//		System.out.println("ip:" + getHostIP());
//		Monitor m =  new Monitor();
//		m.run();
//		
//	}

}
